package cn.msqweb.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.msqweb.constant.ManagerConstants;
import cn.msqweb.entity.SysRoleMenu;
import cn.msqweb.mapper.SysRoleMenuMapper;
import cn.msqweb.service.SysRoleMenuService;
import cn.msqweb.util.AuthUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.msqweb.entity.SysRole;
import cn.msqweb.service.SysRoleService;
import cn.msqweb.mapper.SysRoleMapper;
import jakarta.annotation.Resource;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
* @author 20120
* @description 针对表【sys_role(角色)】的数据库操作Service实现
* @createDate 2024-06-19 18:15:11
*/
@Service(value = "sysRoleService")
@CacheConfig(cacheNames = "cn.msqweb.service.impl.SysRoleServiceImpl")
public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRole>
    implements SysRoleService{

    @Resource
    private SysRoleMapper sysRoleMapper;

    @Resource
    private SysRoleMenuService sysRoleMenuService;

    @Resource
    private SysRoleMenuMapper sysRoleMenuMapper;



    @Override
    @Cacheable(key = ManagerConstants.SYS_ROLE_KEY)
    public List<SysRole> querySysRoleList() {
        return sysRoleMapper.selectList(new LambdaQueryWrapper<SysRole>()
                .orderByDesc(SysRole::getCreateTime)
        );
    }

    @Override
    @Transactional
    @CacheEvict(key = ManagerConstants.SYS_ROLE_KEY)
    public boolean saveSysRole(SysRole sysRole) {
        sysRole.setCreateTime(new Date());
        sysRole.setCreateUserId(AuthUtils.getLoginSecurityUserID());
        int insert = sysRoleMapper.insert(sysRole);
        Long roleId = sysRole.getRoleId();
        if (insert>0){
            List<Long> menuIdList = sysRole.getMenuIdList();
            List<SysRoleMenu> sysRoleMenuList = new ArrayList<>();
            if (CollectionUtil.isNotEmpty(menuIdList)){
                menuIdList.forEach(menuIdList1->{
                    SysRoleMenu sysRoleMenu = new SysRoleMenu();
                    sysRoleMenu.setMenuId(menuIdList1);
                    sysRoleMenu.setRoleId(roleId);
                    sysRoleMenuList.add(sysRoleMenu);
                });
            }
            boolean b = sysRoleMenuService.saveBatch(sysRoleMenuList);
        }



        return insert>0;
    }

    @Override
    @Transactional
    @CacheEvict(key = ManagerConstants.SYS_ROLE_KEY)
    public Boolean modifySysRole(SysRole sysRole) {
        Long roleId = sysRole.getRoleId();
        // 删除原有的角色与权限关系记录
        sysRoleMenuMapper.delete(new LambdaQueryWrapper<SysRoleMenu>()
                .eq(SysRoleMenu::getRoleId,roleId)
        );
        // 获取权限id集合
        List<Long> menuIdList = sysRole.getMenuIdList();
        // 循环遍历
        List<SysRoleMenu> sysRoleMenuList = new ArrayList<>();
        menuIdList.forEach(menuId -> {
            // 创建角色与权限关系记录
            SysRoleMenu sysRoleMenu = new SysRoleMenu();
            sysRoleMenu.setRoleId(roleId);
            sysRoleMenu.setMenuId(menuId);
            sysRoleMenuList.add(sysRoleMenu);
        });
        // 批量添加角色与权限关系
        sysRoleMenuService.saveBatch(sysRoleMenuList);
        // 修改角色信息
        return sysRoleMapper.updateById(sysRole)>0;
    }

    @Override
    public SysRole querySysRoleDetailById(Long roleId) {
        // 根据标识查询角色信息
        SysRole sysRole = sysRoleMapper.selectById(roleId);
        // 根据标识查询角色与权限关系集合
        List<SysRoleMenu> sysRoleMenuList = sysRoleMenuMapper.selectList(new LambdaQueryWrapper<SysRoleMenu>()
                .eq(SysRoleMenu::getRoleId, roleId)
        );
        // 判断是否有值
        if (CollectionUtil.isNotEmpty(sysRoleMenuList) && sysRoleMenuList.size() != 0) {
            // 从角色与权限关系集合中获取权限id集合
            List<Long> menuIdList = sysRoleMenuList.stream().map(SysRoleMenu::getMenuId).collect(Collectors.toList());
            sysRole.setMenuIdList(menuIdList);
        }
        return sysRole;
    }

    @Override
    @Transactional
    @CacheEvict(key = ManagerConstants.SYS_ROLE_KEY)
    public Boolean removeSysRoleByIds(List<Long> roleIds) {
        // 删除角色与权限关系记录
        sysRoleMenuMapper.delete(new LambdaQueryWrapper<SysRoleMenu>()
                .in(SysRoleMenu::getRoleId,roleIds)
        );
        // 删除角色
        return sysRoleMapper.deleteBatchIds(roleIds)==roleIds.size();
    }
}




